package application.mvc.sys.config;

import java.net.URL;
import java.util.ResourceBundle;

import application.common.util.LayerUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class ConfigController implements Initializable{

	@FXML
	private Label headerLabel;
	
	@FXML
	private Label copyrightLabel;
	
	@FXML
	private Label usernameLabel;
	
	@FXML
	private Label roleNameLabel;
	
	@FXML
	private ImageView portraitImageView;
	
	@FXML
	private Pane timerOutLinePane;
	@FXML
	private Pane timerInLinePane;
	@FXML
	private Label timerLabel;
	private Timeline animation;
	private RotateTransition rotateTransitionOut;
	private RotateTransition rotateTransitionIn;
	
	private boolean isDestroy=false;
	
	public void initialize(URL location, ResourceBundle resources) {
		
		headerLabel.setText(ShiroUtil.getSoftName());
		copyrightLabel.setText(ShiroUtil.getCopyright());
		
		if(ShiroUtil.getUser()!=null) {
			portraitImageView.setImage(ShiroUtil.getUser().getPortrait());
			usernameLabel.setText(ShiroUtil.getUser().getName());
			roleNameLabel.setText(ShiroUtil.getUser().getRoleName());
		}
		
		if(!isDestroy) {
			rotateTransitionOut = new RotateTransition(Duration.seconds(10), timerOutLinePane);
			rotateTransitionOut.setFromAngle(0);
			rotateTransitionOut.setToAngle(360);
			rotateTransitionOut.setByAngle(360); // 设置旋转的角度
			rotateTransitionOut.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
			rotateTransitionOut.setInterpolator(Interpolator.LINEAR);
			rotateTransitionOut.play();

			rotateTransitionIn = new RotateTransition(Duration.seconds(10), timerInLinePane);
			rotateTransitionIn.setFromAngle(360);
			rotateTransitionIn.setToAngle(0);
			rotateTransitionIn.setByAngle(360); // 设置旋转的角度
			rotateTransitionIn.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
			rotateTransitionIn.setInterpolator(Interpolator.LINEAR);
			rotateTransitionIn.play();
			startCountDown();
		}
	}
	
	public void startCountDown() {
		animation = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(RouteUtil.getTime()>0) {
					RouteUtil.setTime(RouteUtil.getTime() - 1);
					if (RouteUtil.getTime() == 0) {
						timerLabel.setText("0");
						LayerUtil.open("操作超时！", 999);
					} else if (RouteUtil.getTime() > 0) {
						timerLabel.setText(RouteUtil.getTime() + "");
					}
				}
			}
		}));
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();
	}

	public void destroy() {
		if (animation != null) {
			animation.stop();
		}
		if (rotateTransitionOut != null) {
			rotateTransitionOut.stop();
		}
		if (rotateTransitionIn != null) {
			rotateTransitionIn.stop();
		}
	}
	
	public void base(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			RouteUtil.goBaseConfig();
		}
	}
	
	public void users(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			RouteUtil.goUsers();
		}
	}
	
	public void cameras(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			RouteUtil.goSettingCamera();
		}
	}
	
	public void logs(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			RouteUtil.goFault();
		}
	}
	
	public void cabinets(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			RouteUtil.goSettingBox();
		}
	}
	
	public void exit(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			destroy();
			if(ShiroUtil.getUser()!=null) {
				RouteUtil.goArchives();
			}else {
				RouteUtil.goInit();
			}
		}
	}

}
